﻿<prism:VisualStateAwarePage 
    x:Name="pageRoot"
    x:Class="Sumc.Client.ModernUI.Views.SchedulePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Sumc.Client.ModernUI.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:prism="using:Microsoft.Practices.Prism.StoreApps"
    xmlns:behaviors="using:Sumc.Client.ModernUI.Attachements"
    xmlns:converters="using:Sumc.Client.ModernUI.Converters"
    xmlns:controls="using:Sumc.Client.ModernUI.Controls"
    mc:Ignorable="d"
    prism:ViewModelLocator.AutoWireViewModel="True">

    <prism:VisualStateAwarePage.Resources>
        <DataTemplate x:Key="StopItemTemplate">
            <Grid Width="300"
                  Background="{StaticResource DefaultItemBackground}"
                  Margin="1"
                  behaviors:Behaviours.PointerPressed="{Binding DataContext.GoToScheduleInformation, ElementName=pageRoot}"
                  behaviors:Behaviours.PointerPressedParameter="{Binding ScheduleUrl}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Image Source="ms-appx:///Assets/Icons/BusStop.png"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Center"
                       Margin="20"
                       Stretch="None"/>
                <StackPanel Grid.Column="1"
                            Margin="0,20,20,20">
                    <TextBlock Foreground="{StaticResource DefaultWhiteBrush}"
                               FontSize="18"
                               TextWrapping="NoWrap"
                               TextTrimming="WordEllipsis"
                               Text="{Binding Name}">
                    </TextBlock>
                    <TextBlock FontSize="14"
                               Margin="0"
                               Foreground="{StaticResource DefaultDarkBrush}"
                               TextWrapping="NoWrap">
                        <Run x:Uid="SchedulePageRunNumber" />
                        <Run Text="{Binding Number}" />
                    </TextBlock>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </prism:VisualStateAwarePage.Resources>
    <prism:VisualStateAwarePage.TopAppBar>
        <AppBar>
            <Button Style="{StaticResource HomeAppBarButtonStyle}" 
                    Command="{Binding GoToHome}" />
        </AppBar>
    </prism:VisualStateAwarePage.TopAppBar>
    <prism:VisualStateAwarePage.BottomAppBar>
        <AppBar IsSticky="{Binding IsBottomAppBarSticky, Mode=TwoWay}"
                IsOpen="{Binding IsBottomAppBarOpened, Mode=TwoWay}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                
                <StackPanel Visibility="{Binding IsFound, Converter={StaticResource BooleanToVisibilityConverter}}"
                        Orientation="Horizontal">
                    <Button x:Name="PinAppBarButton"
                        x:Uid="PinAppBarButton"
                        AutomationProperties.AutomationId="PinAppBarButton"
                        Command="{Binding PinTile}"
                        Style="{StaticResource PinAppBarButtonStyle}"
                        Visibility="{Binding IsPinnedTile, Converter={StaticResource BooleanToVisibilityConverter},ConverterParameter=1}"
                        Loaded="StartLayoutUpdates"
                        Unloaded="StopLayoutUpdates" />
                    <Button x:Name="UnPinAppBarButton"
                        x:Uid="UnPinAppBarButton"
                        AutomationProperties.AutomationId="UnPinAppBarButton"
                        Command="{Binding UnpinTile}"
                        Visibility="{Binding IsPinnedTile, Converter={StaticResource BooleanToVisibilityConverter}}"
                        Loaded="StartLayoutUpdates"
                        Unloaded="StopLayoutUpdates"
                        Style="{StaticResource UnPinAppBarButtonStyle}"/>
                </StackPanel>
                <StackPanel Grid.Column="1"
                            Orientation="Horizontal"
                            HorizontalAlignment="Right">
                    <Button Command="{Binding SaveSchedule}"
                            Style="{StaticResource SaveAppBarButtonStyle}"
                            HorizontalAlignment="Right"/>
                    <Button Command="{Binding OpenRightTimes}"
                            Style="{StaticResource GoAppBarButtonStyle}"
                            HorizontalAlignment="Right"/>
                </StackPanel>
            </Grid>
        </AppBar>
    </prism:VisualStateAwarePage.BottomAppBar>

    <Grid Style="{StaticResource LayoutRootStyle}"
          Background="{StaticResource Background}"
          x:Name="MainGrid">
        <Grid.RowDefinitions>
            <RowDefinition Height="140" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <!--No Information Message-->
        <Grid Margin="120,200,0,0"
              Grid.RowSpan="2"
              Visibility="{Binding IsFound, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=1}">
            <TextBlock FontSize="18"
                       Foreground="{StaticResource DefaultWhiteBrush}"
                       x:Name="ErrorTextBlock"
                       Visibility="{Binding LoadingData, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=1}"
                       Text="{Binding ErrorMessage}"/>
        </Grid>

        <ScrollViewer x:Name="MainScrollViewer"
                      Grid.RowSpan="2"
                      Padding="0,170,0,50"
                      HorizontalScrollBarVisibility="Auto"
                      HorizontalScrollMode="Enabled"
                      VerticalScrollMode="Disabled"
                      VerticalScrollBarVisibility="Disabled"
                      MaxZoomFactor="1"
                      MinZoomFactor="0.5">
            <StackPanel x:Name="ResultsStackPanel"
                        Orientation="Horizontal"
                        Margin="120,0,120,0">
                <Grid x:Name="TransportsGrid"
                      Visibility="{Binding ArrivalTimes,Converter={StaticResource NullToVisibilityConverter}}"
                      Margin="0,0,100,0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock FontSize="18"
                               Margin="0,0,0,35"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               x:Uid="SchedulePageTextBlockSchedule" />
                    <controls:AlternatingListView x:Name="TransportsGridView"
                                                  Grid.Row="1"
                                                  Grid.Column="0" 
                                                  ItemsSource="{Binding ArrivalTimes, Mode=OneWay}"
                                                  SelectionMode="None" ItemContainerStyle="{StaticResource ListViewItemSomething}">
                        <controls:AlternatingListView.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Margin="0" Padding="10" FontSize="18" Width="70" Text="{Binding}" />
                            </DataTemplate>
                        </controls:AlternatingListView.ItemTemplate>
                    </controls:AlternatingListView>
                </Grid>

                <Grid Visibility="{Binding Stops,Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock FontSize="18"
                               Margin="5,0,0,35"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               x:Uid="SchedulePageTextBlockOtherRelatedStops" />
                    <GridView x:Name="StopsGridView"
                              Grid.Row="1"
                              Grid.Column="0"
                              ItemsSource="{Binding Stops, Mode=OneWay}"
                              ItemTemplate="{StaticResource StopItemTemplate}"
                              SelectionMode="None"
                              Style="{StaticResource GridViewWithoutScroll}" />
                </Grid>

            </StackPanel>
        </ScrollViewer>
        <!--Snap-->
        <ScrollViewer x:Name="SnapMainScrollViewer"
                      Visibility="Collapsed"
                      Grid.RowSpan="2"
                      Padding="5,170,0,0"
                      HorizontalScrollBarVisibility="Disabled"
                      HorizontalScrollMode="Disabled"
                      VerticalScrollMode="Enabled"
                      VerticalScrollBarVisibility="Auto"
                      MaxZoomFactor="1"
                      MinZoomFactor="0.5">
            <StackPanel x:Name="SnapResultsStackPanel"
                        Orientation="Vertical">
                <Grid x:Name="SnapTransportsGrid"
                      Visibility="{Binding ArrivalTimes,Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock FontSize="18"
                               Margin="5,0,0,30"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               x:Uid="SchedulePageTextBlockSchedule" />
                    <controls:AlternatingListView x:Name="SnapTransportsListView"
                                                  Grid.Row="1"
                                                  Grid.Column="0" 
                                                  ItemsSource="{Binding ArrivalTimes, Mode=OneWay}"
                                                  SelectionMode="None" ItemContainerStyle="{StaticResource ListViewItemSomething}"
                                                  Style="{StaticResource ListViewWithoutScroller}">
                        <controls:AlternatingListView.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Margin="0" Padding="10" FontSize="18" Text="{Binding}" />
                            </DataTemplate>
                        </controls:AlternatingListView.ItemTemplate>
                    </controls:AlternatingListView>
                </Grid>

                <Grid Visibility="{Binding Stops,Converter={StaticResource NullToVisibilityConverter}}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock FontSize="18"
                               Margin="5,50,0,30"
                               Foreground="{StaticResource DefaultWhiteBrush}"
                               TextWrapping="Wrap"
                               x:Uid="SchedulePageTextBlockOtherRelatedStops" />
                    <GridView x:Name="SnapStopsGridView"
                              Grid.Row="1"
                              Grid.Column="0"
                              ItemsSource="{Binding Stops, Mode=OneWay}"
                              ItemTemplate="{StaticResource StopItemTemplate}"
                              SelectionMode="None"
                              Style="{StaticResource GridViewWithoutScroll}" />
                </Grid>

            </StackPanel>
        </ScrollViewer>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton"
                    Command="{Binding GoBack}"
                    Style="{StaticResource BackButtonStyle}" />
            <Image x:Name="AppLogoImage"
                   Grid.Column="1" Source="ms-appx:///Assets/MainLogo.png"
                   Stretch="None"
                   HorizontalAlignment="Left" NineGrid="0" VerticalAlignment="Top" Margin="0,50,0,0"/>
            <TextBlock Visibility="{Binding Path=Title, Converter={StaticResource NullToVisibilityConverter}}"
                       x:Name="PageTitle"
                       Grid.Column="3"
                       Style="{StaticResource PageHeaderTextStyle}" HorizontalAlignment="Left" Margin="20,0,30,40">
                <Run Text="{Binding TransportType, Converter={StaticResource TransportTypeToCyrillicConverter}}" />
                <Run Text="{Binding Title}" />
                <Run Text=" - " />
                <Run Text="{Binding Stop.Name}"/>
            </TextBlock>
        </Grid>

        <ProgressBar IsIndeterminate="{Binding LoadingData}"
                     Visibility="{Binding LoadingData, Converter={StaticResource BooleanToVisibilityConverter}}"
                     Style="{StaticResource PageProgressBar}"
                     Grid.Row="1" />

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape" />
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource PortraitBackButtonStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SnapTransportsGrid"
                                                       Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="5" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ErrorTextBlock"
                                                       Storyboard.TargetProperty="Margin">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="0,200,0,0" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MainScrollViewer"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Collapsed" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SnapMainScrollViewer"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Visible" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource SnappedBackButtonStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PageTitle"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource SnappedPageHeaderTextStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="AppLogoImage"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Collapsed" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</prism:VisualStateAwarePage>
